package com.ruoyi.content.controller;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import com.ruoyi.content.domain.Tag;
import com.ruoyi.content.service.ITagService;
import org.slf4j.Logger;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.content.domain.Article;
import com.ruoyi.content.service.IArticleService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.util.ShiroUtils;
import org.springframework.web.multipart.MultipartFile;

/**
 * 文章 信息操作处理
 * 
 * @author cloudoer
 * @date 2019-06-25
 */
@Controller
@RequestMapping("/content/article")
public class ArticleController extends BaseController
{
	private static final Logger log = LoggerFactory.getLogger(ArticleController.class);

    private String prefix = "content/article";
	
	@Autowired
	private IArticleService articleService;

	@Autowired
	private ITagService tagService;
	
	@RequiresPermissions("content:article:view")
	@GetMapping()
	public String article()
	{
	    return prefix + "/article";
	}
	
	/**
	 * 查询文章列表
	 */
	@RequiresPermissions("content:article:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Article article)
	{
		startPage();
        List<Article> list = articleService.selectArticleList(article);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出文章列表
	 */
	@RequiresPermissions("content:article:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Article article)
    {
    	List<Article> list = articleService.selectArticleList(article);
        ExcelUtil<Article> util = new ExcelUtil<Article>(Article.class);
        return util.exportExcel(list, "article");
    }
	
	/**
	 * 新增文章
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存文章
	 */
	@RequiresPermissions("content:article:add")
	@Log(title = "文章", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(Article article)
	{
		article.setState("0");
		article.setAuthor(ShiroUtils.getSysUser().getUserName());
		article.setPostTime(new Date());
		article.setRanking("0");
		return toAjax(articleService.insertArticle(article));
	}

	/**
	 * 修改文章
	 */
	@GetMapping("/edit/{articleId}")
	public String edit(@PathVariable("articleId") Long articleId, ModelMap mmap)
	{
		List<Tag> tags = tagService.selectTagList(new Tag());
		Article article = articleService.selectArticleById(articleId);
		mmap.put("article", article);
		mmap.put("tags", tags);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存文章
	 */
	@RequiresPermissions("content:article:edit")
	@Log(title = "文章", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(Article article, Long[] list)
	{
		article.setUpdateBy(ShiroUtils.getSysUser().getUserName());
		article.setUpdateTime(new Date());
		return toAjax(articleService.updateArticle(article, list));
	}
	
	/**
	 * 删除文章
	 */
	@RequiresPermissions("content:article:remove")
	@Log(title = "文章", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(articleService.deleteArticleByIds(ids));
	}

	/**
	 * 修改头图
	 * @param mmap
	 * @return
	 */
	@GetMapping("/img/{aid}")
	public String img(ModelMap mmap, @PathVariable("aid") Long aid){
		Article article = articleService.selectArticleById(aid);
		mmap.put("article", article);
		return prefix + "/img";
	}

	@Log(title = "文章管理", businessType = BusinessType.UPDATE)
	@PostMapping("/img/{aid}")
	@ResponseBody
	public AjaxResult img(@RequestParam("articleImg")MultipartFile file, @PathVariable("aid") Long aid){
		String imgSrc = "";
		try {
			imgSrc = articleService.img(aid, file);
			Article article = new Article();
			article.setArticleId(aid);
			article.setImage(imgSrc);
			articleService.updateArticle(article, new Long[]{});
		} catch (IOException e) {
			log.error("修改文章头图失败！", e);
			return error(e.getMessage());
		}
		AjaxResult result = success();
		result.put("imgSrc", imgSrc);
		return result;
	}

	@Log(title = "文章管理", businessType = BusinessType.UPDATE)
	@PostMapping("/aImg/{aid}")
	@ResponseBody
	public AjaxResult aImg(@RequestParam("articleImg")MultipartFile file, @PathVariable("aid") Long aid){
		String imgSrc = "";
		try {
			imgSrc = articleService.img(aid, file);
		} catch (IOException e) {
			log.error("上传文章图片失败！", e);
			return error(e.getMessage());
		}
		AjaxResult result = success();
		result.put("errno", 0);
		result.put("data", new String[]{imgSrc});
		return result;
	}
}
